OneHot ================= 根据输入的索引值,在指定轴上生成独热编码(one-hot encoding)。 .. math:: output_{i_1, i_2, \dots, i_{axis}, \dots, i_n} = \begin{cases} on\_value, & \text{if } i_{axis} = indices_{j} \\ off\_value, & \text{otherwise} \end{cases} 其中,:math:`indices_{j}` 为输入索引张量的元素,索引 :math:`j` 与输出下标 :math:`(i_1, \dots, i_{axis-1}, i_{axis+1}, \dots, i_n)` 一一对应。 :math:`depth` 表示 one-hot 向量的长度,:math:`axis` 表示新维度插入的位置。 当启用 ``support_neg_index`` 时,若 :math:`indices_{j} < 0`,则执行如下修正: .. math:: indices_{j} = indices_{j} + depth 输入: - **on_off** - 包含**on_value**和**off_value**,表示独热编码中“热”和“冷”位置的值。 - **axis** - 指定生成独热编码的轴。 - **depth** - 独热编码的深度。 - **support_neg_index** - 是否支持负索引。 - **indices_shape_size** - 索引形状的维度大小。 - **indices_shape** - 索引的形状数组地址。 - **indices** - 索引值地址。 - **core_mask** - 核心掩码,指定使用的计算核心。 输出: - **output** - 生成的独热编码结果地址。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 支持int8, int16, int32, fp32, fp64, cplx64, cplx128 - MT7004 支持fp16, fp32, int16, int32, cplx64 - MT6678 调用时将 axis/depth/support_neg_index/indices_shape_size打包通过 int_param 数组传入 **共享存储版本:** .. c:function:: void i8_onehot_s(int8_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int8_t *output, int core_mask) .. c:function:: void i16_onehot_s(int16_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int16_t *output, int core_mask) .. c:function:: void i32_onehot_s(int *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int *output, int core_mask) .. c:function:: void hp_onehot_s(half *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, half *output, int core_mask) .. c:function:: void fp_onehot_s(float *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output, int core_mask) .. c:function:: void dp_onehot_s(double *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output, int core_mask) .. c:function:: void c64_onehot_s(float* on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output, int core_mask) .. c:function:: void c128_onehot_s(double* on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 13 //FT78NE示例 #include int main(int argc, char* argv[]) { float on_off[2] = {1.0, 0.0}; int axis=1, depth=4, indices_shape_size=2; int support_neg_index=1; int int_param[4] = {axis, depth, support_neg_index, indices_shape_size}; int indices_shape[2] = {3, 3}; int *indices = (int *)0xA0000000; // indices在DDR空间 float *output = (float*)0xB0000000; // indices_shape[0] * depth * indices_shape[1] int core_mask = 0xff; fp_onehot_p(on_off, int_param, indices_shape, indices, output, core_mask); // 7004调用方式: // fp_onehot_p(on_off, axis, depth, support_neg_index, indices_shape_size, indices_shape, indices, output, core_mask); return 0; } **私有存储版本:** .. c:function:: void i8_onehot_p(int8_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int8_t *output) .. c:function:: void i16_onehot_p(int16_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int16_t *output) .. c:function:: void i32_onehot_p(int *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int *output) .. c:function:: void hp_onehot_p(half *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, half *output) .. c:function:: void fp_onehot_p(float *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output) .. c:function:: void dp_onehot_p(double *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output) .. c:function:: void c64_onehot_p(float* on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output) .. c:function:: void c128_onehot_p(double* on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 12 //FT78NE示例 #include int main(int argc, char* argv[]) { float on_off[2] = {1.0, 0.0}; int axis=1, depth=4, indices_shape_size=2; int support_neg_index=1; int int_param[4] = {axis, depth, support_neg_index, indices_shape_size}; int indices_shape[2] = {3, 3}; int *indices = (int *)0x10000000; // indices在L2空间 float *output = (float*)0x10001000; // indices_shape[0] * depth * indices_shape[1] fp_onehot_p(on_off, int_param, indices_shape, indices, output); // 7004调用方式: // fp_onehot_p(on_value, off_value, axis, depth, support_neg_index, indices_shape_size, indices_shape, indices, output); return 0; }